import machine 
from machine import Pin,network,PWM
import time
run_led=PWM(Pin(2), freq=1000, duty=0)
PWM_r=PWM(Pin(5), freq=1000, duty=0)
PWM_g=PWM(Pin(3), freq=1000, duty=0)
PWM_b=PWM(Pin(4), freq=1000, duty=0)
horn=Pin(5,Pin.OUT)
horn.value(0)
buten=Pin(6,Pin.IN,Pin.PULL_UP)
ssid='esp_main'
password='8191031520'
def run_led_on():#板载运行指示灯
    for i in range(0,1024,100):
        run_led.duty(i)
        time.sleep(0.01)
def wifi_on():
    ap_if = network.WLAN(network.AP_IF)
    ap_if.config(essid=ssid, password=password)
    ap_if.active(True)
    print('AP started')
    if ap_if.active():True
    for i in range(300,1024,1):
        PWM_b.duty(i)
        time.sleep(0.01)
    for i in range(1023,300,-1):
        PWM_b.duty(i)
        time.sleep(0.01)
        print('AP started')
        return True
    else:
        
        for i in range(300,1024,1):
            PWM_r.duty(i)
            time.sleep(0.01)
        for i in range(1023,300,-1):
            PWM_r.duty(i)
            time.sleep(0.01)
            print('AP not started')
            return False
def duty_cls():
    PWM_r.duty(0)
    PWM_g.duty(0)
    PWM_b.duty(0)
def button_off():
    if buten.value()==0:
        stime=time.ticks_ms()
        while buten.value()==0:
            if time.ticks_ms()-stime>2000:
                return True

while True:
    run_led()
    if button_off()==True:
        for i in range(300,1024,50):
            PWM_b.duty(i)
            time.sleep(0.01)
        for i in range(1023,300,-50):
            PWM_b.duty(i)
            time.sleep(0.01)
        duty_cls()
        wifi_on()
        if wifi_on()==True:
           for i in range(300,1024,50):
                PWM_b.duty(i)
                time.sleep(0.01)
                for i in range(1023,300,-50):
                    PWM_b.duty(i)
                    time.sleep(0.01)

        